package com.ruiboyun.liveapp.util.tool;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by ${吴世芊} on $2016062016/6/2.
 * 浙江答分奇网络科技有限公司
 * 本类功能:
 */
public class DateUtils {
    public static final String[] zodiacArray = { "猴", "鸡", "狗", "猪", "鼠", "牛",
            "虎", "兔", "龙", "蛇", "马", "羊" };
    public static final String[] monthArray = { "一月", "二月", "三月", "四月", "五月", "六月",
            "七月", "八月", "九月", "十月", "十一月", "十二月" };

    public static final String[] constellationArray = { "水瓶座", "双鱼座", "牡羊座",
            "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };

    public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22,
            23, 23, 23, 23, 22, 22 };

    /**
     * 根据日期获取生肖
     *
     * @return
     */
    public static String date2Zodica(Calendar time) {
        return zodiacArray[time.get(Calendar.YEAR) % 12];
    }
    public static String getBitDecimal(double number){
        BigDecimal bigDecimal = new BigDecimal(number);
        String s = bigDecimal.toPlainString();
//        moneyNum = (float) balance.getData();
        Double doule = Double.parseDouble(s);
        String result = String.format("%.1f", doule);
        return result;
    }
    /**
     * 根据日期获取生肖
     *
     * @return
     */
    public static String date2Zodica(String time) {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date = sdf.parse(time);
            c.setTime(date);

            String zodica = date2Zodica(c);
            System.out.println("生肖：" + zodica);
            return zodica;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据日期获取星座
     *
     * @param time
     * @return
     */
    public static String date2Constellation(Calendar time) {
        int month = time.get(Calendar.MONTH);
        int day = time.get(Calendar.DAY_OF_MONTH);
        if (day < constellationEdgeDay[month]) {
            month = month - 1;
        }
        if (month >= 0) {
            return constellationArray[month];
        }
        // default to return 魔羯
        return constellationArray[11];
    }
    /**
     * 根据日期获取星座
     *
     * @param time
     * @return
     */
    public static String date2Constellation(String time) {

        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        Date date;
        try {
            date = sdf.parse(time);
            c.setTime(date);

            String constellation = date2Constellation(c);
            System.out.println("星座：" + constellation);
            return constellation;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }
    public static int getYear(){
        Calendar calendar= Calendar.getInstance();
        return  calendar.get(Calendar.YEAR);
    }
    public static int getYearInt(){
        Calendar calendar= Calendar.getInstance();
        return  calendar.get(Calendar.YEAR);
    }
    public static String getMonth(int month){

        if (month>9){
            return String.valueOf(month);
        }else {
            return "0"+ String.valueOf(month);
        }
    }

    public static String getMonth(){
        Calendar calendar= Calendar.getInstance();
        if (calendar.get(Calendar.MONTH)>9){
            return String.valueOf(calendar.get(Calendar.MONTH)+1);
        }else {
            return "0"+ String.valueOf(calendar.get(Calendar.MONTH)+1);
        }
    } public static String getMonthString(){
        Calendar calendar= Calendar.getInstance();
        return monthArray[calendar.get(Calendar.MONTH)];

    }

    public static int getMonthInt(){
        Calendar calendar= Calendar.getInstance();
        return calendar.get(Calendar.MONTH)+1;
    }
    public static String getDay(){
        Calendar calendar= Calendar.getInstance();
        if (calendar.get(Calendar.DAY_OF_MONTH)>9){
            return String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        }else {
            return "0"+ String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
        }

    }
    public static int getDayInt(){
        Calendar calendar= Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_MONTH);

    }
    public static String getHours(){
        Calendar calendar= Calendar.getInstance();
        return  String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
    }
    public static int getHourInt(){
        Calendar calendar= Calendar.getInstance();
        return  calendar.get(Calendar.HOUR_OF_DAY);
    }
    public static String getMinite(){
        Calendar calendar= Calendar.getInstance();
        return  String.valueOf(calendar.get(Calendar.MINUTE));
    }

    public static int getMinuteInt() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    public static int getSecond() {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }



    public static String getWeek(){
        Date date=new Date();
        SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
        dateFm.format(date);
        return date.toString();
    }
    /**
     * 获取当前日期是星期几<br>
     *
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate() {
        Date date=new Date();
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
    public static int getWeekInt(){
        Date date=new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK)-1;

        return w;
    }

    public static String long2StrDate(long time){
        Date date = new Date(time);
        SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return dateFm.format(date);
    }
    /**
     * String日期转换为Long     * @param formatDate("MM/dd/yyyy HH:mm:ss")     * @param date("12/31/2013 21:08:00")     * @return     * @throws ParseException
     */
    public static Long transferStringDateToLong(String formatDate, String date)  {
        SimpleDateFormat sdf = new SimpleDateFormat(formatDate);
        Date dt = null;
        try {
            dt = sdf.parse(date);
            return dt.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return System.currentTimeMillis()+50*1000;
        }

    }
    /**
     * String日期转换为Long     * @param formatDate("MM/dd/yyyy HH:mm:ss")     * @param date("12/31/2013 21:08:00")     * @return     * @throws ParseException
     */
    public static Long transferStringDateToLong(String date)  {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = null;
        try {
            dt = sdf.parse(date);
            return dt.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return System.currentTimeMillis()+50*1000;
        }

    }
    public static String strToString(String date){
        // 准备第一个模板，从字符串中提取出日期数字
        String pat1 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ;
        // 准备第二个模板，将提取后的日期数字变为指定的格式
        String pat2 = "yyyy.MM.dd HH:mm" ;
        SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ;        // 实例化模板对象
        SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ;        // 实例化模板对象
        Date d = null ;
        try{
            d = sdf1.parse(date) ;   // 将给定的字符串中的日期提取出来
            Date date1=new Date(d.getTime()+8*60*60*1000);
            // 将日期变为新的格式
            return sdf2.format(date1);
        }catch(Exception e){            // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace() ;       // 打印异常信息
            Loger.e(""+e.toString());
            return "";
        }
    }
    public static String strToString2(String date){
        // 准备第一个模板，从字符串中提取出日期数字
        String pat1 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ;
        // 准备第二个模板，将提取后的日期数字变为指定的格式
        String pat2 = "yyyy.MM.dd WXEntryActivity hh:mm" ;
        SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ;        // 实例化模板对象
        SimpleDateFormat sdf2 = new SimpleDateFormat(pat2,Locale.ENGLISH) ;        // 实例化模板对象
        Date d = null ;
        try{
            d = sdf1.parse(date) ;   // 将给定的字符串中的日期提取出来
            Date date1=new Date(d.getTime()+8*60*60*1000);
            // 将日期变为新的格式
            return sdf2.format(date1);
        }catch(Exception e){            // 如果提供的字符串格式有错误，则进行异常处理
            e.printStackTrace() ;       // 打印异常信息
            Loger.e(""+e.toString());
            return "";
        }
    }

    /**
     *
     * @param date 07-07/2016/19:12
     * @return
     */
    public static boolean isDeliverPass(String date){
        return System.currentTimeMillis()>DateUtils.transferStringDateToLong(date)?true:false;
    }
    /**
     *
     * @param date 07-07/2016/19:12
     * @return
     */
    public static boolean isPass(String date){
        return System.currentTimeMillis()>DateUtils.transferStringDateToLong("MM-dd/yyyy/HH:mm",date)?true:false;
    }
}
